<?php
require_once('header.php');
$template->assign('script', 'news');


//View News Page
if(isset($_GET['nid']) && !empty($_GET['nid']))
{
	
    //Latest 3 News
	$sql2 = mysql_query("SELECT * FROM news $where ORDER BY id DESC LIMIT 3");
	
	if(mysql_num_rows($sql2))
	{
		while($nrow = mysql_fetch_object($sql2))
		{
			
			$id = $nrow->id;
			$title = stripslashes($nrow->title);
			$written_by =  stripslashes($nrow->written_by);
			$description = stripslashes($nrow->description);
			$date = stripslashes(date('M d, Y' ,strtotime($nrow->date)));
			$img = stripslashes($nrow->img);
			
			if(strlen($description) > 205)
			$description = substr($description, 0, 202)."...";
			
			if(empty($img))
			$img = SITE_URL."templates/images/defaultNews.png";
			
			$news[] = array('id' => $id, 'title' => $title, 'written_by' => $written_by, 'description' => $description, 'date' => $date, 'img' => $img);
			
		}
		
		$template->assign('news', $news);
	}	
	//End Latest 3 News	
	
		
	$nid = makesafe(safeGet($_GET['nid']));
	$template->assign('nid', $nid);
	
	if(isset($_POST['postComment']))
	{


		if($Auth->checkAuth())
		{
			$uid = $Auth->getLoggedId();
			$username = getUserById($uid);		
			$comment = makesafe($_POST['comment']);
			$comment  = strip_tags($comment);
			
			if(mysql_query("INSERT INTO news_comments VALUES(NULL, '$nid', '$username', '$comment', NOW())"))
			{
			    header("location: news.php?nid=$nid");	
				exit;
			}else
			{
			    $template->assign('c_error', "Comment couldn't be added. Try later");	
			}
		}

		
	}
	
	
	$nsql = mysql_query("SELECT * FROM news WHERE id = '$nid' LIMIT 1");
	if(mysql_num_rows($nsql))
	{
	    $nro = mysql_fetch_object($nsql);
		$new_title = stripslashes($nro->title);
		$news_description = stripslashes($nro->description);
		$author = stripslashes($nro->written_by);
		$date = date("F d, Y", strtotime($nro->date));
		$img = stripslashes($nro->img);
		$date_day = stripslashes(date('jS' ,strtotime($nro->date)));
  	    $date_month = stripslashes(date('M' ,strtotime($nro->date)));	
		$date_year = stripslashes(date('Y' ,strtotime($nro->date)));		
		
		
		$template->assign('date_day', $date_day);
		$template->assign('date_month', $date_month);
		$template->assign('date_year', $date_year);
		$template->assign('nid', $nid);		
		$template->assign('title', $new_title);
		$template->assign('description', $news_description);
		$template->assign('author', $author);
		$template->assign('date', $date);						
		$template->assign('img', $img);								
	
	
	
	    //get comments for this news
		$ncq = mysql_query("SELECT * FROM news_comments WHERE news_id = '$nid'");
		if(mysql_num_rows($ncq))
		{
		    while($ncr = mysql_fetch_object($ncq))
			{
				$cid = $ncr->id;
				$u = stripslashes($ncr->user);
				$m = stripslashes($ncr->message);
				$d = date('F jS, Y', strtotime($ncr->date));
				$t = date('g:ia',  strtotime($ncr->date));
				
				$comments[] = array('user' => $u, 'comment' => $m, 'date' => $d, 'time' => $t);
			}	
			
			$template->assign('comments', $comments);
		}
	
  	    $template->display('view_news.tpl.php');	
		return;	
	}
	
    
}
//end view News page

if(isset($_GET['q']) && !empty($_GET['q']))
{
    $q = makesafe($_GET['q']);	
	$where  = "WHERE title LIKE '%$q%' OR description LIKE '%$q%' ";
}

$self = "news.php";
$pagetitle = "News & Annoucements";
$page = 1;
$showPerPage = 10;


if(isset($_GET['page']) && !empty($_GET['page']))
{
    $page = (int) makesafe($_GET['page']);
    $page = abs($page);
}


$offset = ceil($page-1)*$showPerPage;

$sql1 = mysql_query("SELECT COUNT(id) as total FROM news $where");
if(mysql_num_rows($sql1))
{
    $r = mysql_fetch_object($sql1);
	$records = $r->total;
}

if($records < 1)
{
   
   $msg = "No Annoucement yet";
   $template->assign('msg', $msg);
   $template->display('news.tpl.php');
   return;
}

$pages = ceil($records/$showPerPage);

if($page > $pages)
{
   header("location: $self?page=$pages");
}

$sql2 = mysql_query("SELECT * FROM news $where ORDER BY id DESC LIMIT $offset, $showPerPage");

if(mysql_num_rows($sql2))
{
	while($nrow = mysql_fetch_object($sql2))
	{
		
		$id = $nrow->id;
		$title = stripslashes($nrow->title);
		$written_by =  stripslashes($nrow->written_by);
		$description = stripslashes($nrow->description);
		$date_day = stripslashes(date('jS' ,strtotime($nrow->date)));
  	    $date_month = stripslashes(date('M' ,strtotime($nrow->date)));
		$img = stripslashes($nrow->img);
		
	  if(empty($img))
	  $img = SITE_URL."templates/images/defaultNews.png";		
		
		if(strlen($description) > 205)
		$description = substr($description, 0, 202)."...";
		
		$news[] = array('id' => $id, 'title' => $title, 'written_by' => $written_by, 'description' => $description, 'date_day' => $date_day, 'date_month' => $date_month, 'img' => $img);
		
	}
}


//Paging
//next
if($pages > $page)
$next = $page+1;

//previous
if($pages >= $page && $page > 1)
$previous = $page-1;

//last
if($pages > 1 && $page != $pages){
$last = $pages;
}

//first
if($page > 1 && $pages > 1)
$first = 1;


//paging numbers
if($pages <= 5 && $page <= 5)
{
     $i = 1;
     while($i<=$pages)
	 {
		 $pnums[] = $i;
		 $i++;
	 }
	
}elseif($pages > 5)
{
  
  
   if($pages-1 == 5 && $page <= 5){
	   
     $i = 1;
     while($i<=$pages)
	 {
		 $pnums[] = $i;
		 $i++;
		 
		 if($i == 6)
		 break;
	 } 
  
	   
	   

   }else
   {


  
	   if($page != 1){   
	   if($page-2 >= 1)
		   $pnums[] = $page-2;
	   elseif($page <= 2)
		   $pnums[] = 1;
	   }
	   
	   
	   
	   if($page-1>=1)
	   $pnums[] = $page-1;   
	   
	   if($page == $pages);
	   $pnums[] = $page;
	   
       if($page == 1){
		   
	   if($pages > 1)
	   $pnums[] = $page+1;

	   if($pages > 2)
	   $pnums[] = $page+2;

		   
	   }
	   
	   if($page < $pages)
	   {
		   if($page == $pages)
		   $pnums[] = $page;
		   else
		   {	
	   
	           if(max($pnums)+1 != $pages-1 && max($pnums)+1 != $pages){   
			   if(max($pnums) < $pages)
				$pnums[] = "...";
				$dots = max($pnums)+1;
			   }
	
		
		}
	   }
	  
	   
	   if($pages-2 > $page)
	   $pnums[] = $pages-2;
	
	   if($pages-1 > $page)
	   $pnums[] = $pages-1;   
	   
   }
 	
}else
{
	
}



$template->assign('page', $page);
$template->assign('pages', $pages);
$template->assign('pnums', $pnums);
$template->assign('newP', $dots);
$template->assign('first', $first);
$template->assign('previous', $previous);
$template->assign('next', $next);
$template->assign('last', $last);

$template->assign('news', $news);
$template->display('news.tpl.php');
?>